﻿using System;
using System.Globalization;

namespace SoulCard
{
    /// <summary>
    /// 数据
    /// </summary>
    [Serializable]
    public class CardData
    {
        /// <summary>
        /// 索引
        /// </summary>
        public string Id;

        /// <summary>
        /// 置顶索引
        /// </summary>
        public bool SetTop;

        /// <summary>
        /// 内容
        /// </summary>
        public string Content;

        /// <summary>
        /// 是否完成
        /// </summary>
        public bool Complete;

        /// <summary>
        /// 创建时间
        /// </summary>
        public string CreatedAt;

        /// <summary>
        /// 修改时间
        /// </summary>
        public string ModifiedAt;

        public DateTime ModifiedDateTime
        {
            get
            {
                return !string.IsNullOrEmpty(ModifiedAt)
                              ? DateTime.ParseExact(ModifiedAt, "yyyy/MM/dd HH:mm:ss", CultureInfo.CurrentCulture)
                                  : default(DateTime);
            }
        }

        public string ModifiedText
        {
            get
            {
                var modifiedDateTime = ModifiedDateTime;

                var distance = DateTime.Now - modifiedDateTime;

                if (distance.TotalSeconds < 60)
                {
                    return string.Format("{0} 秒前", (int)distance.TotalSeconds);
                }

                if (distance.TotalMinutes < 60)
                {
                    return string.Format("{0} 分钟前", (int)distance.TotalMinutes);
                }

                if (distance.TotalHours < 24)
                {
                    return string.Format("{0} 小时前", (int)distance.TotalHours);
                }

                if (distance.TotalDays < 7)
                {
                    return string.Format("{0} 天前", (int)distance.TotalDays);
                }

                if (distance.TotalDays > 7)
                {
                    return string.Format("{0} 周前", (int)(distance.TotalDays / 7));
                }

                if (distance.TotalDays > 30)
                {
                    return string.Format("{0} 月前", (int)(distance.TotalDays / 30));
                }

                if (distance.TotalDays > 365)
                {
                    return string.Format("{0} 年前", (int)(distance.TotalDays / 365));
                }

                return ModifiedAt;
            }
        }

        public CardData(string id)
        {
            Id = id;
        }
    }
}